home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 1.iso / dist / fw_exmh.idb / usr / freeware / lib / exmh-2.5 / receipt.tcl.z / receipt.tcl
Text File  |  2002-07-08  |  16KB  |  559 lines

  1. # receipt.tcl
  2. #
  3. # Handling of message disposition notifications 
  4. #
  5.  
  6. # removes any disposition-notification-to header and puts a brand new one
  7. proc MDNAskReceipt { draft t } {
  8.     global miscRE mime env faces
  9.  
  10.     if {[info exists mime(mdnTo)] && $mime(mdnTo) != {}} {
  11.     set mdnTo $mime(mdnTo)
  12.     } else {
  13.     set host [exec hostname]
  14.     set domain $faces(defaultDomain)
  15.  
  16.     if {$domain == ""} {
  17.         catch {set domain [exec domainname]'}
  18.     }
  19.     if {$domain == ""} {
  20.         catch {set domain [exec grep domain /etc/resolv.conf | cut -f2 -d\ ]'}
  21.     }
  22.     if {$domain == ""} {
  23.         set domain "PLEASE CONFIGURE DISPOSITION NOTIFICATION UNDER PREFS/MIME"
  24.     }
  25.     set mdnTo $env(USER)@$host.$domain
  26.     }
  27.  
  28.     SeditSave $draft $t
  29.  
  30.     set linenb 1
  31.     set line [$t get $linenb.0 $linenb.end]
  32.  
  33.     while {![regexp $miscRE(headerend) $line]} {
  34.     if [regexp -nocase {^disposition-notification-to:} $line] {
  35.         set line " dummy"
  36.         while {[regexp "^\[ \t]" $line]} {
  37.         $t delete $linenb.0 [expr {$linenb + 1}].0
  38.         set line [$t get $linenb.0 $linenb.end]
  39.         }
  40.     } else {
  41.         set linenb [expr {$linenb + 1}]
  42.     }
  43.     set line [$t get $linenb.0 $linenb.end]
  44.     }
  45.  
  46.     $t insert 1.0 "Disposition-Notification-To: $mdnTo\n"
  47. }
  48.  
  49. proc MDNGenerate { file address choice mode } {
  50.     global exwin mimeHdr
  51.  
  52.     if {$choice != "ignored"} {
  53.     set mdnfile [MDNBuildDraft $file $address $choice $mode]
  54.     if [catch {exec send -nopush $mdnfile} result] {
  55.       Exmh_Debug "send result: $result"
  56.       Exmh_Status "Could not send message disposition notification" error
  57.       return
  58.     }
  59.     }
  60.     MDNAddHeaderToDraft $file "X-ExmhMDN: $choice"
  61.     MsgShowInText $exwin(mtext) $mimeHdr(0,rawfile)
  62. }
  63.  
  64.  
  65. proc MDNAddHeaderToDraft { draft header } {
  66.     if [catch {open $draft} in] {
  67.     error "Cannot read draft to add header"
  68.     }
  69.     if [catch {open $draft.new w} out] {
  70.     close $in
  71.     error "Cannot add header"
  72.     }
  73.     set state header
  74.     for {set len [gets $in line]} {! [eof $in]} {set len [gets $in line]} {
  75.     if {$state == "header"} {
  76.         if {$len == 0 || [regexp ^-- $line]} {
  77.         set state body
  78.         puts $out $header
  79.         }
  80.     }
  81.     puts $out $line
  82.     } 
  83.  
  84.     close $out
  85.     close $in
  86.     Mh_Rename $draft.new $draft
  87. }
  88.  
  89. proc MDNAsk {tkw address explain} {
  90.     global mimeHdr exmh
  91.  
  92.     $tkw insert insert "  The sender wants you to acknowledge that you have\
  93.     seen this mail."
  94.     if {[string compare $explain {}] != 0} {
  95.     $tkw insert insert "\n\n  NOTE! For the reason(s) listed below, it may\
  96.             be unsafe to send the disposition notification.  Please check the\
  97.             message carefully.  Unless you are sure that it is safe to send\
  98.             the notification, press \"Ignore silently\""
  99.     $tkw insert insert $explain
  100.     }
  101.  
  102.     $tkw insert insert "\n\n  Do you want to send a disposition notification\
  103.         (receipt) to \n      $address?"
  104.     $tkw insert insert "\n\n       "
  105.     TextButton $tkw " Send confirmation " \
  106.     [list MDNGenerate $mimeHdr(0,rawfile) \
  107.          $mimeHdr(0=1,hdr,disposition-notification-to) \
  108.          "displayed" "manual-action/MDN-sent-manually"]
  109.     $tkw insert insert "    "
  110.     TextButton $tkw " Send denial " \
  111.     [list MDNGenerate $mimeHdr(0,rawfile) \
  112.          $mimeHdr(0=1,hdr,disposition-notification-to) \
  113.          "denied" "manual-action/MDN-sent-manually"]
  114.     $tkw insert insert "    "
  115.     TextButton $tkw " Ignore silently " \
  116.     [list MDNGenerate $mimeHdr(0,rawfile) \
  117.     $mimeHdr(0=1,hdr,disposition-notification-to) \
  118.          "ignored" {}]
  119.     $tkw insert insert "\n"
  120.     MimeInsertSeparator $tkw 0 6
  121. }
  122.  
  123. proc MDNCheck { tkw } {
  124.     global mimeHdr mime
  125.  
  126.     if {![info exists mimeHdr(0=1,hdr,x-exmhmdn)] && \
  127.         [info exists mimeHdr(0=1,hdr,disposition-notification-to)]} {
  128.     if [info exists mime(mdnDone)] {
  129.         unset mime(mdnDone)
  130.     } else {
  131.         set dnt $mimeHdr(0=1,hdr,disposition-notification-to)
  132.  
  133.         switch $mime(mdnSend) {
  134.         "never" {
  135.             set mdnAction1 "ignored"
  136.             set mdnAction2 "ignored"
  137.         }
  138.         "deny" {
  139.             set mdnAction1 "denied"
  140.             set mdnAction2 "ignored"
  141.         }
  142.         "ask user" {
  143.             set mdnAction1 "ask"
  144.             set mdnAction2 "ask"
  145.         }
  146.         "auto/ask" {
  147.             set mdnAction1 "displayed"
  148.             set mdnAction2 "ask"
  149.         }
  150.         "auto/ignore" {
  151.             set mdnAction1 "displayed"
  152.             set mdnAction2 "ignored"
  153.         }
  154.         default {
  155.             set mdnAction1 "ask"
  156.             set mdnAction2 "ask"
  157.         }
  158.         }
  159.  
  160.         set mdnExplain {}
  161.         
  162.         set line [string trim $dnt]
  163.         if [regsub {\(.*\)} $line {} newline] {
  164.         set line $newline
  165.         }
  166.  
  167.         if {[string first "|" $line] != -1} {
  168.         set mdnAction1 "ignore"
  169.         set mdnExplain "$mdnExplain 
  170.  
  171.  * The address(es) for the disposition notification contains a pipe symbol (|)
  172.    *** THIS MAY BE A SERIOUS SECURITY HOLE."
  173.         }
  174.  
  175.         if {[string first "," $line] != -1} {
  176.         set mdnAction1 $mdnAction2
  177.         set mdnExplain "$mdnExplain 
  178.  
  179.  * The sender appears to have requested a disposition notification to be
  180.    sent to more than one address.  If you are not sure that there is a valid
  181.    reason to send disposition notifications to each of these addresses,
  182.    the request should be ignored and no disposition notifications sent."
  183.         }
  184.  
  185.         if [info exists mimeHdr(0=1,hdr,return-path)] {
  186.         if {[string compare \
  187.              [MsgParseFrom $mimeHdr(0=1,hdr,return-path) {}] \
  188.              [MsgParseFrom $line {}]] != 0} {
  189.             set mdnAction1 $mdnAction2
  190.             set mdnExplain "$mdnExplain
  191.  
  192.  * The disposition notification appears to be directed somewhere else than
  193.    to the sender of the message.  If you are not sure that there is a valid
  194.    reason for this, the request should be ignored and no disposition
  195.    notifications sent."
  196.         }
  197.         } else {
  198.         set mdnAction1 $mdnAction2
  199.         set mdnExplain "$mdnExplain
  200.  
  201.  * The message does not have a Return-path header field, and therefore it
  202.    is not possible to verify that the disposition notification address(es)
  203.    is valid."
  204.         }
  205.  
  206.         if [info exists mimeHdr(0=1,hdr,disposition-notification-options)]\
  207.         {
  208.         set mdnAction1 $mdnAction2
  209.         set mdnExplain "$mdnExplain
  210.  
  211.  * The message has a Disposition-notification-options header requesting
  212.    some special processing which exmh does not know about."
  213.         if [regexp -nocase {=[ ]*required[ ]*,} \
  214.             $mimeHdr(0=1,hdr,disposition-notification-options)] {
  215.             set mdnAction1 "ignored"
  216.             set mdnExplain "$mdnExplain
  217.  
  218.    Since one or more of the unknown options are required to be taken into
  219.    account for generating a proper disposition notification, no disposition
  220.    notification at all should be generated."
  221.         }
  222.         }
  223.  
  224.         if {[string compare $mdnAction1 "ask"] == 0} {
  225.         MDNAsk $tkw $dnt $mdnExplain
  226.         } else {
  227.         MDNGenerate $mimeHdr(0,rawfile) $dnt $mdnAction1 \
  228.             "manual-action/MDN-sent-automatically"
  229.         }
  230.     }
  231.     }
  232. }
  233.  
  234. proc MDNExplainDisposition { tkw reportVar } {
  235.     upvar $reportVar report
  236.  
  237.     set disp $report(disposition)
  238.  
  239.     $tkw insert insert "The disposition code is:
  240.     $disp
  241. which means:
  242. "
  243.  
  244.     if [regsub -all {(\(.*\))|([     ]+)} $disp {} newline] {
  245.     set disp $newline
  246.     }
  247.     set disp [string tolower $disp]
  248.  
  249.     if [regexp {^([-a-z]+)/([-a-z]+);([-a-z]+)(/(.*))?$} $disp match \
  250.         action_mode sending_mode disp_type match2 disp_modifiers] {
  251.  
  252.     switch $action_mode {
  253.         "manual-action" {
  254.         $tkw insert insert "
  255.     The recipient acted manually on the message:\n"
  256.         }
  257.         "automatic-action" {
  258.         $tkw insert insert "
  259.     The recipient's computer system has been set up to act automatically
  260.     on incoming messages:\n"
  261.         }
  262.         default {
  263.         $tkw insert insert "
  264.     Unable to tell whether the message was acted on manually or automatically.
  265.     The code describing the action is:  $action_mode,
  266.     which is not a valid code.\n"
  267.         }
  268.     }
  269.  
  270.     switch $disp_type {
  271.         "displayed" {
  272.         $tkw insert insert "
  273.         The mail was displayed by the user agent to someone reading the
  274.         recipient's mailbox.  (This does not guarantee that it is read
  275.         or understood.)"
  276.         }
  277.         "denied" {
  278.         $tkw insert insert "
  279.         The recipient does not wish you to be informed of the message's
  280.         disposition."
  281.         }
  282.         "dispatched" {
  283.         $tkw insert insert "
  284.         The mail has been sent somewhere (e.g. printed, faxed, forwarded)
  285.         without being displayed to the user.  (The user may or may not see
  286.         the message later.)"
  287.             }
  288.         "processed" {
  289.         $tkw insert insert "
  290.         The message has been processed in some manner (i.e. by some sort of
  291.         rules or server) without being displayed to the user.  (The user may
  292.         or may not see the message later, or there may not even be a human
  293.         user associated with the mailbox.)"
  294.             }
  295.             "failed" {
  296.         $tkw insert insert "
  297.         A failure occurred that prevented the proper generation of an MDN."
  298.         if [info exists report(failure)] {
  299.             $tkw insert insert "
  300.  
  301.         The reason for the failure was:
  302.              $report(failure)"
  303.         }
  304.             }
  305.         "deleted" {
  306.         $tkw insert insert "
  307.         The message has been deleted.  (The recipient may or may not have
  308.         seen the message.  The recipient might \"undelete\" the message at a
  309.         later time and read the message.)"
  310.             }
  311.             default {
  312.         $tkw insert insert "
  313.         Unknown disposition type $disp_type."
  314.            }
  315.         }
  316.  
  317.     while {[string length $disp_modifiers] > 0} {
  318.         set sep [string first "," $disp_modifiers]
  319.         if { $sep < 0 } {
  320.         set modifier $disp_modifiers
  321.         set disp_modifiers ""
  322.         } else {
  323.         set modifier \
  324.             [string range $disp_modifiers 0 [expr $sep - 1]]
  325.         set disp_modifiers \
  326.             [string range $disp_modifiers \
  327.              [expr $sep + 1] \
  328.              [string length $disp_modifiers]]
  329.         }
  330.         switch $modifier {
  331.         "error" {
  332.             $tkw insert insert "
  333.  
  334.         An error of some sort occurred that prevented successful processing of
  335.     the message.  "  
  336.             if [info exists report(error)] {
  337.             $tkw insert insert "Error message:
  338.             $report(error)"
  339.             } else {
  340.             $tkw inser insert "(No error message given)"
  341.             }
  342.         }
  343.         "warning" {
  344.             $tkw insert insert "
  345.  
  346.         The message was successfully processed but some sort of exceptional
  347.         condition occurred.  "
  348.             if [info exists report(warning)] {
  349.             $tkw insert insert "Warning message:
  350.             $report(warning)"
  351.             } else {
  352.             $tkw inser insert "(No warning message given)"
  353.             }
  354.         }
  355.         "superseded" {
  356.             $tkw insert insert "
  357.  
  358.         The message has been automatically rendered obsolete by another 
  359.         message received.  (The recipient may still access and read the
  360.         message later.)"
  361.         }
  362.         "expired" {
  363.             $tkw insert insert "
  364.  
  365.         The message has reached its expiration date and has been automatically
  366.         removed from the recipient's mailbox."
  367.         }
  368.         "mailbox-terminated" {
  369.             $tkw insert insert "
  370.  
  371.         The recipient's mailbox has been terminated and all messages in it 
  372.         automatically removed."
  373.         }
  374.         default {
  375.         }
  376.         }
  377.     }
  378.  
  379.     switch $sending_mode {
  380.         "mdn-sent-manually" {
  381.         $tkw insert insert "
  382.  
  383.     The recipient manually sent (or confirmed that the user agent could send)
  384.     this MDN."
  385.         }
  386.         "mdn-sent-automatically" {
  387.         $tkw insert insert "    \
  388.  
  389.     This MDN was generated automatically (with no explicit manual 
  390.     confirmation by the recipient)."
  391.         }
  392.         default {
  393.         $tkw insert insert "
  394.  
  395.     The way the MDN was sent is described as: $sending_mode
  396.     (which is not a valid code)."
  397.         }
  398.     }
  399.     } else {
  400.     switch $disp {
  401.         "displayed" {
  402.         $tkw insert insert "
  403.     The mail was displayed by the user agent to someone reading the 
  404.     recipient's mailbox.  This does not guarantee that it is read or
  405.     understood."
  406.         }
  407.         "denied" {
  408.         $tkw insert insert "
  409.     The recipient does not wish you to be informed of the message's 
  410.     disposition."
  411.         }
  412.         "processed" {
  413.         $tkw insert insert "
  414.     The message has been processed in some manner (e.g. printed, faxed,
  415.     forwarded) in response to a user command, without being displayed to the
  416.     user.  The user may or may not see the message later."
  417.         }
  418.         "autoprocessed" {
  419.         $tkw insert insert "
  420.     The message has been processed automatically in some manner (e.g. printed,
  421.     faxed, forwarded, gatewayed) in response to some user request made in
  422.     advance, without being displayed to the user.  The user may or may not 
  423.     see the message later."
  424.         }
  425.         "deleted" {
  426.         $tkw insert insert "
  427.     The message has manually been deleted.  The recipient may or may not have
  428.     seen the message."
  429.         }
  430.         "autodeleded" {
  431.         $tkw insert insert "
  432.     The message has been automatically deleted without being displayed to the
  433.     recipient."
  434.         }
  435.         "obsoleted" {
  436.         $tkw insert insert "
  437.     The message has been automatically rendered obsolete by another message
  438.     received.  The recipient may still access and read the message later."
  439.         }
  440.         "terminated" {
  441.         $tkw insert insert "
  442.     The recipient's mailbox has been terminated and all messages in it
  443.     automatically deleted."
  444.         }
  445.         "autodenied" {
  446.         $tkw insert insert "
  447.     The recipient does not wish the sender to be informed of the message's
  448.     disposition, and has requested that this MDN be sent automatically."
  449.         }
  450.         default {
  451.         $tkw insert insert "
  452.     The format of the disposition code is not recognized, 
  453.     cannot explain it further."
  454.       }
  455.       }
  456.     }
  457.     $tkw insert insert "\n\n"
  458. }
  459.  
  460. proc MDNBuildDraft { draft address doit choice} {
  461.     global env mimeHdr faces exmh
  462.     set host [exec hostname]
  463.  
  464.     # If /bin/hostname has a '.' in it, assume it's already a FQDN.
  465.     if [ regexp {\.} $host ] {
  466.         set sourcehost $host
  467.     } else { #otherwise, try to find a domain from $faces or resolv.conf
  468.         set domain $faces(defaultDomain)
  469.         if {$domain == ""} {
  470.         catch {set domain [exec grep domain /etc/resolv.conf | cut -f2 -d\ ]'}
  471.         }
  472.     # Try this last, as YP domainname may not match actual DNS domain
  473.     if {$domain == ""} {
  474.         catch {set domain [exec domainname]}
  475.     }
  476.         set sourcehost $host.$domain
  477.     }
  478.     set rcpt $env(USER)@$sourcehost
  479.  
  480.     if [catch {open $draft} in] {
  481.     error "Cannot read original message"
  482.     }
  483.     set mdn [Mime_TempFile mdn]
  484.     if [catch {open $mdn w} out] {
  485.     close $in
  486.     error "Cannot create mdn"
  487.     }
  488.     # Bug - someplace right here, we need to make 'post' generate
  489.     # a 'MAIL FROM:<>' to be fully RFC compliant.,..
  490.     puts $out "Subject: Disposition notification\nTo: $address"
  491.  
  492.     set bdry [FvMimeStartMulti $out \
  493.     "multipart/report; report-type=disposition-notification" 0]
  494.  
  495.     FvMimeAddPart $out $bdry ""
  496.  
  497.     if {$doit == "displayed"} {
  498.     puts $out "
  499. The message below has been displayed to $rcpt.
  500. This is no guarantee that it has been read or understood.
  501. "
  502.     } else {
  503.     puts $out "
  504. The recipient of the message below did not wish the sender to be informed
  505. of the message's disposition.
  506. "
  507.     }
  508.     FvMimeAddPart $out $bdry "message/disposition-notification"
  509.     puts $out "\nReporting-UA: $sourcehost (Exmh $exmh(version))"
  510.     if [info exists mimeHdr(0=1,hdr,original-recipient)] {
  511.     puts $out "Original-Recipient: $mimeHdr(0=1,hdr,original-recipient)"
  512.     }
  513.     puts $out "Final-Recipient: rfc822; $rcpt"
  514.     if [info exists mimeHdr(0=1,hdr,message-id)] {
  515.     puts $out "Original-Message-ID: $mimeHdr(0=1,hdr,message-id)"
  516.     }
  517.     puts $out "Disposition: $choice; $doit\n"
  518.  
  519.     FvMimeAddPart $out $bdry "message/rfc822\n"
  520.  
  521.     for {gets $in line} {! [eof $in]} {gets $in line} {
  522.     puts $out $line
  523.     } 
  524.     puts $out "--$bdry--"
  525.     close $out
  526.     close $in
  527.     return $mdn
  528. }
  529.  
  530. proc ExtractAddress { string } {
  531.     if {[scan $string "%\[^<]%c%\[^>]" * * address] != 3} {
  532.     return $string
  533.     }
  534.     return $address
  535. }
  536.  
  537.  
  538. #I use these procedures, which should become part of seditMime.tcl
  539. #but are still part of something I use on the side -Brent
  540.  
  541. proc FvMimeStartMulti {out contentType level} {
  542.     set boundary [SeditBoundary $out $level]
  543.     puts $out "Mime-Version: 1.0"
  544.     puts $out "Content-Type: $contentType;\n\tboundary=\"$boundary\""
  545.     puts $out "\nMultipart\n"
  546.     return $boundary
  547. }
  548. proc FvMimeAddPart {out boundary contentType} {
  549.     puts $out "--$boundary"
  550.     if {$contentType != ""} {
  551.     puts $out "Content-Type: $contentType"
  552.     } else {
  553.     puts $out ""
  554.     }
  555. }
  556. proc FvMimeEndMulti {out boundary} {
  557.     puts $out "--$boundary--"
  558. }
  559.